﻿using BLL;
using Common;
using IBLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1.登录老师账号界面
{
    
    public partial class AlterStudent : Form
    {
        List<Students_MD> Students = null;
        IFrStudentManage_IBLL frStudent = new FrStudentManage_BLL();
        int id = 0;
        public AlterStudent(int id)
        {
            InitializeComponent();
            //初始化班级
            comboClass.DataSource = frStudent.SClass();
            comboClass.DisplayMember = "ClassName";
            comboClass.ValueMember = "ClassId";
           this.id=id;
            initialize();
        }
        public AlterStudent(int id,string l)
        {
            InitializeComponent();
            //初始化班级
            comboClass.DataSource = frStudent.SClass();
            comboClass.DisplayMember = "ClassName";
            comboClass.ValueMember = "ClassId";
            this.id = id;
            #region 查看学生信息
            label7.Text = "查看学生信息";
            AddStudentss.Visible = false;
            textBox1.ReadOnly = true;
            Gender.Enabled = false;
            dtmBrithday.Enabled = false;
            pictureBox1.Enabled = false;
            comboClass.Enabled = false;
            txtStudentIdNo.Enabled = false;
            txtAddress.Enabled = false;
            txtPhoneNum.Enabled = false;
            button1.Visible = false;
            button2.Visible = false;
            #endregion
            initialize();
        }
        //初始化考生信息
        public void initialize()
        {
            Students = frStudent.Students($"where StudentId='{id}'");
            textBox1.Text = Students[0].StudentName;
            if (Students[0].Gender == "男") { radioMan.Checked = true; } else { radioGirl.Checked = true; }
            dtmBrithday.Value = Convert.ToDateTime(Students[0].Birthday);
            txtPhoneNum.Text = Students[0].PhoneNumber;
            comboClass.Text = Students[0].ClassName;
            txtStudentIdNo.Text = Students[0].StudentIdNo;
            txtAddress.Text = Students[0].StudentAddress;
            pictureBox1.Image = Students[0].StuImage == "" ? null : (Image)Common_Com.DeSerializeObjevtToString(Students[0].StuImage);
        }
        private void butReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|Windows Bitmap(*.bmp)|*.bmp|Windows Icon(*.ico)|*.ico|Graphics Interchange Format (*.gif)|(*.gif)|JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|Portable Network Graphics (*.png)|*.png|Tag Image File Format (*.tif)|*.tif;*.tiff";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            }

        }
        //更新考生信息
        private void AddStudentss_Click(object sender, EventArgs e)
        {
           
            Students_MD Students_MD= new Students_MD() {
                     StudentName = textBox1.Text,
                     Gender = radioMan.Checked == true ? "男" : "女",
                     Birthday = dtmBrithday.Value.ToString(),
                     PhoneNumber = txtPhoneNum.Text,
                     ClassId = (int)comboClass.SelectedValue,
                     ClassName = comboClass.Text,
                     StudentIdNo = txtStudentIdNo.Text,
                     StudentAddress = txtAddress.Text,
                     StuImage = pictureBox1.Image == null ? null :Common_Com.SerializeObjevtToString(pictureBox1.Image).ToString(),
            };
            //验证格式
            string Sinfo = verificationPassWrod_com.addVerify(Students_MD);
            if (Sinfo!=null)
            {
                MessageBox.Show(Sinfo, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return;
            }
            try
            {
                //提交
                frStudent.AlterStudent(Students_MD, id.ToString());
                MessageBox.Show("考生信息修改成功", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            catch (Exception ex)
            {
                MessageBox.Show("考生信息修改失败 错误信息："+ex.Message,"温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Question);
            }
                
      
        }
     
    }
}
